﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimioAPI;
using SimioAPI.Extensions;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;


namespace WLC
{
    class WLCUnstoreIntermediateProductStepDefinition : IStepDefinition
    {
        #region IStepDefinition Members

        public IStep CreateStep(IPropertyReaders properties)
        {
            return new WLCUnstoreIntermediateProductStep(properties);
        }

        public void DefineSchema(IPropertyDefinitions schema)
        {
            IPropertyDefinition orderID = schema.AddExpressionProperty("OrderID", string.Empty);
            orderID.Description = "The order for which to unstore an item (-1 for freely available products).";
            orderID.Required = true;

            IPropertyDefinition orderBOM = schema.AddElementProperty("OrderBOM", WLCBOMElementDefinition.ID);
            orderBOM.Description = "The BOM element of the order to determine which items must be unstored.";
            orderBOM.Required = true;

            IPropertyDefinition orderSize = schema.AddExpressionProperty("OrderSize", string.Empty);
            orderSize.Description = "The size of the order. This value is multiplied with each BOM entry.";
            orderSize.Required = true;

            IPropertyDefinition intermediateProductStore = schema.AddElementProperty("IntermediateProductStore", WLCIntermediateProductStoreElementDefinition.ID);
            intermediateProductStore.Description = "The product store to unstore from.";
            intermediateProductStore.Required = true;
        }

        public int NumberOfExits
        {
            get { return 1; }
        }

        public string Name
        {
            get { return "UnstoreIntermediateProduct"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return null; }
        }

        public Guid UniqueID
        {
            get { return new Guid("{A6B2E8DA-1B46-11DF-956D-9F2256D89593}"); }
        }

        #endregion
    }


    class WLCUnstoreIntermediateProductStep : IStep
    {

        IPropertyReader _orderIDProp;
        IElementProperty _orderBOM;
        IPropertyReader _orderSize;
        IElementProperty _productStoreProp;


        public WLCUnstoreIntermediateProductStep(IPropertyReaders bag)
        {
            _orderBOM = (IElementProperty)bag.GetProperty("OrderBOM");
            _orderSize = bag.GetProperty("OrderSize");
            _orderIDProp = bag.GetProperty("OrderID");
            _productStoreProp = (IElementProperty)bag.GetProperty("IntermediateProductStore");
        }

        #region IStep Members

        private string GetOrderType(IStepExecutionContext context)
        {
            string rawType = context.AssociatedObject.ExecutionContext.ToString();
            string[] splitType = rawType.Split(' ');


            return splitType[1];
        }

        public ExitType Execute(IStepExecutionContext context)
        {


            WLCBOMElement bom = (WLCBOMElement)_orderBOM.GetElement(context);

           
            // Get the order size from the property
            double orderSize = (double)_orderSize.GetDoubleValue(context);

            // Get the order ID to unstore this product for
            string orderID = GetOrderType(context) + "." + ((int)_orderIDProp.GetDoubleValue(context)).ToString();

            WLCIntermediateProductStoreElement productStore = (WLCIntermediateProductStoreElement)_productStoreProp.GetElement(context);

            Hashtable bomEntries = bom.getBOMEntries();
            double currentAmount;

            BOMEntry entry;

            foreach (string key in bomEntries.Keys)
            {
                entry = (BOMEntry)bomEntries[key];
                currentAmount = entry.Amount;

                Debug.WriteLine("Time: " + context.Calendar.TimeNow.ToString() + ", Trying to unstore" + currentAmount * orderSize + " units of item " + key+" for order id: " + orderID);
                
                double reply = productStore.Withdraw(orderID, key, currentAmount * orderSize);
                Debug.WriteLine("Time: " + context.Calendar.TimeNow.ToString() + ", Answer from store: " + reply);
                
            }
            
            return ExitType.FirstExit;
        }

        #endregion

    }


}
